package com.iss.okair.service.trans;

import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsNormal;
import com.iss.okair.db.entity.AssetsTranUse;
import com.iss.okair.db.entity.Assetstrans;
import com.iss.okair.db.mapper.AssetsNornalMapper;
import com.iss.okair.db.mapper.NormalTransMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Service
public class NormalTransService {
    @Resource
    private NormalTransMapper mapper;
    @Resource
    private AssetsNornalMapper nmapper;
    public List<AssetsTranUse> find(AssetsTranUse trans){
        return mapper.select(trans);
    }
    public List<AssetsTranUse> findout(AssetsTranUse trans){
        return mapper.selectout(trans);
    }

    public boolean add(Assetstrans trans) {
        trans.setId(UuidTools.buildUUID());
        String asid=trans.getAssetsid();
        String str=this.buildBathNo(asid);
        trans.setBatchno(str);
        trans.setTranstype("1");
        int temp=mapper.add(trans);
        return temp>=1?true:false;
    }
    private String buildBathNo(String id){
        AssetsNormal normal=nmapper.selectById(id);
        String code=normal.getAssetscode();
        Calendar cal= Calendar.getInstance();
        String time=cal.getTimeInMillis()+"";
        return String.format("%s-%s",code,time);
    }
    //记录
    public List<AssetsTranUse> Record(String assetsid){
        return  mapper.Record(assetsid);
    }
    //领用和转移操作
    public boolean Use(String type,Assetstrans finance){
        //领用动作,出库

        if ("2".equals(type)){
            finance.setTranstype(type);
            return  mapper.add(finance)>=1 ? true:false;
        }else if("4".equals(type)) {
            //转移动作
            finance.setTranstype(type);
            return mapper.add(finance)>=1 ? true:false;
        }else {
            return false;
        }
    }
}
